<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="货名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入货名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="仓库" prop="storageId" class="custom-select">
        <!-- <el-input v-model="queryParams.storageId" placeholder="请输入仓库" clearable @keyup.enter="handleQuery" /> -->
        <el-select v-model="queryParams.storageId" placeholder="请选择仓库" clearable>
          <el-option v-for="item in storageList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="goodstypeId" class="custom-select">
        <!-- <el-input v-model="queryParams.goodstypeId" placeholder="请输入分类" clearable @keyup.enter="handleQuery" /> -->
        <el-select v-model="queryParams.goodstypeId" placeholder="请选择分类" clearable>
          <el-option v-for="item in goodStypeList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
        <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain :icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain :icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain :icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" prop="id" width="50" />
      <el-table-column label="货名" align="center" prop="name" />
      <!-- 根据仓库id展示仓库的名称 -->
      <el-table-column label="仓库" align="center" prop="storageId">
        <template #default="scope">
          <div v-for="item in storageList" :key="item.id">
            <span v-if="item.id == scope.row.storageId">{{ item.name }}</span>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="分类" align="center" prop="goodstypeId" /> -->
      <el-table-column label="分类" align="center" prop="goodstypeId">
        <template #default="scope">
          <div v-for="item in goodStypeList" :key="item.id">
            <span v-if="item.id == scope.row.goodstypeId">{{ item.name }}</span>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="数量" align="center" prop="count" /> -->
      <el-table-column label="数量" align="center" prop="count">
        <template #default="scope">
          <el-tag v-if="scope.row.count > 0" type="success">{{ scope.row.count }}</el-tag>
          <el-tag v-else type="danger">{{ scope.row.count }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="价格" align="center" prop="price"/> -->
       <el-table-column label="价格" align="center" prop="price">
        <template #default="scope">
          <el-tag v-if="scope.row.price > 0" type="success">￥{{ scope.row.price.toFixed(2) }}</el-tag>
          <el-tag v-else type="danger">￥{{ scope.row.price}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="photo" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" :icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" :icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="goodsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="货名" prop="name">
          <el-input v-model="form.name" placeholder="请输入货名" />
        </el-form-item>
        <el-form-item label="仓库" prop="storageId">
          <!-- <el-input v-model="form.storageId" placeholder="请输入仓库" /> -->
          <el-select v-model="form.storageId" placeholder="请选择仓库">
            <el-option v-for="item in storageList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="分类" prop="goodstypeId">
          <!-- <el-input v-model="form.goodstypeId" placeholder="请输入分类" /> -->
          <el-select v-model="form.goodstypeId" placeholder="请选择分类">
            <el-option v-for="item in goodStypeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="count">
          <el-input v-model="form.count" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input-number v-model="form.price" :min="1" :precision="2">
            <template #prefix>
              <span>￥</span>
            </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="图片" prop="photo">
          <image-upload v-model="form.photo" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Goods">
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/goods";
import { Edit, Delete, Plus, Search, Refresh } from '@element-plus/icons-vue';
import { listGoodStype } from "@/api/goodStype";
import { listStorage } from "@/api/storage";
import { loadAllParams } from "@/api/page"


const { proxy } = getCurrentInstance();

const goodsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    storageId: null,
    goodstypeId: null,
  },
  rules: {
    name: [
      { required: true, message: "货名不能为空", trigger: "blur" }
    ],
    storageId: [
      { required: true, message: "仓库不能为空", trigger: "blur" }
    ],
    goodstypeId: [
      { required: true, message: "分类不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商品列表 */
function getList() {
  loading.value = true;
  listGoods(queryParams.value).then(response => {
    goodsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    storageId: null,
    goodstypeId: null,
    count: null,
    remark: null,
    price: null,
    photo: null
  };
  proxy.resetForm("goodsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商品";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getGoods(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商品";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["goodsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateGoods(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGoods(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除商品编号为"' + _ids + '"的数据项？').then(function () {
    return delGoods(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/goods/export', {
    ...queryParams.value
  }, `goods_${new Date().getTime()}.xlsx`)
}

/**查询仓库列表 */
const storageList = ref([]);
function getStorageList() {
  listStorage(loadAllParams).then(response => {
    storageList.value = response.rows;
  });
}

/** 查询分类列表 */
const goodStypeList = ref([]);
function getGoodStypeList() {
  listGoodStype(loadAllParams).then(response => {
    goodStypeList.value = response.rows;
  });
}

getGoodStypeList();
getStorageList();
getList();
</script>
<style scoped>
.custom-select {
  width: 20%;
  /* 或者指定一个具体的宽度 */
  overflow: auto;
  /* 如果内容超出容器，显示滚动条 */
}
</style>
